/// [Digit factorials](https://projecteuler.net/problem=34)
fn main() {
    let mut list = vec![];
    for n in 1.. {
        if n > 362880 * n.to_string().len() {
            break;
        }
        if digit_factorials(n) {
            list.push(n);
        }
    }
    println!("{:?}", list);
    println!("sum: {}", list.iter().sum::<usize>());
}

fn digit_factorials(n: usize) -> bool {
    let buff = [1_usize, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
    if n > 10 {
        let mut count = [0; 10];
        for i in n.to_string().chars().map(|x| x.to_string().parse::<usize>().unwrap()) {
            count[i as usize] += 1;
        }
        if n == count.iter().enumerate().fold(0, |s, (i, x)| s + buff[i] * x) {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn is_work() {
        assert!(digit_factorials(145));
        assert!(!digit_factorials(1));
        assert!(!digit_factorials(2));
    }
}
